/**
 * EditPictureAction.java
 *
 * $LastChangedBy: allen $
 * $LastChangedDate: 2007-12-06 16:27:23 +0800 $
 * $Revision: 1389 $
 */
package com.myspace.view.picture;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.myspace.common.exception.ApplicationException;
import com.myspace.domain.Picture;
import com.myspace.service.picture.EditPictureService;
import com.myspace.view.common.Constant;
import com.myspace.view.common.DispatchBaseAction;
import com.myspace.view.common.UserUtil;

public class EditPictureAction extends DispatchBaseAction {

	public ActionForward removePicture(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EditPictureForm epForm = (EditPictureForm) form;
		if (!UserUtil.isLogin(request)) {
			return mapping.findForward("login");
		}

		EditPictureService editPictureService = (EditPictureService) getWebApplicationContext()
				.getBean("editPictureService");
		try {
			Picture picture = editPictureService.getPicture(epForm.getPicId());
			if (picture == null) {
				throw new ApplicationException("该图片不存在!");
			}
			if (picture.getUser().getId().longValue() != UserUtil.getUser(
					request).getId().longValue()) {
				throw new ApplicationException("你没有权限删除该图片!");
			}
			editPictureService.delPicture(epForm.getPicId());
			epForm.setPicId(epForm.getNextPicId());
		} catch (Exception e) {
			ActionMessages errors = new ActionMessages();
			getLogger().error(e);
			errors.add(Globals.ERROR_KEY, new ActionMessage(e.getMessage(),
					false));
			this.saveErrors(request, errors);
			return mapping.findForward("error");
		}
		String retPath = "/picture/viewPictureTag.do?picId="
				+ epForm.getPicId() + "&tag="
				+ URLEncoder.encode(epForm.getTag(), Constant.ENCODING);
		return new ActionForward(retPath, true);
	}

}
